/**
 * @Author: Nico
 */

import { processingConstant, getDefaultDate, getComponentDefaulType, getModelByModelTree } from '@ve/utils/utils'
// import cloneDeep from 'lodash/cloneDeep'
import isArray from 'lodash/isArray'
import isEmpty from 'lodash/isEmpty'
//必须带done回调 执行完成调用 vm, action, done
export default function pdaFormItemReset({ vm, action, done }) {
  // console.log('重置表单项！！！！', vm, action)
  const notUseDefaultVal = action.extension?.notUseDefaultVal?.value
  isArray(action.extension.formItemSelect.value) &&
    action.extension.formItemSelect.value.forEach((fromItemId) => {
      const _self = vm.vuiComponents[fromItemId]?.self
      console.log('2023/1/19------------_self', _self, fromItemId, vm.vuiComponents[fromItemId])
      if (_self?.getInfo?.type === 'uiPdaUpload') {
        _self.fileList = []
      }
      if (_self && fromItemId.indexOf('uiDynamicFormLot') > -1) {
        // 如果选择的是批次属性组件 则查找对应的表单组件 然后清空批次属性的值
        const modelTree = vm.$store.state.preview.pages[vm.$store.state.preview.nowPageId]
        let _formModule = getModelByModelTree(_self.formId, modelTree)
        for (let i = 1; i < 49; i++) {
          const indexStr = i < 10 ? `0${i}` : i
          _formModule.data.formData[`LOT${indexStr}`] = ''
        }
      } else if (fromItemId.indexOf('uiPdaSelect') != '-1') {
        // 清空表单里面的字段中文值
        // console.log('清空表单里面的字段中文值', _self)
        _self.formDataModel = ''
        _self.formDataModelLabel = ''
      } else {
        if (_self && _self.setFormDataModel) {
          if (notUseDefaultVal) {
            // 如果不使用默认值 则直接清空值为默认类型空值
            _self.setFormDataModel(getComponentDefaulType(_self.getInfo.type))
          } else {
            // 赋默认值
            if (_self.getInfo.extension?.defaultValue?.value) {
              // console.log(`触发寻找默认值~~~~~~~~${model_info._id} --------------------- ${model_info.extension.defaultValue.value}`)
              _self.setFormDataModel(processingConstant(vm, _self.getInfo.extension.defaultValue.value))
              //选择新
              if (_self.getInfo.type.indexOf('uiSelectLayerNew') !== -1) {
                _self.setFormDataModel(processingConstant(vm, _self.getInfo.extension.defaultValue.value))
              }
            } else {
              //选择新
              if (_self.getInfo.type.indexOf('uiSelectLayerNew') !== -1) {
                // 查找表单项组件所在的表单组件 然后重置表单组件对应的 model 和 text 的绑定值
                const modelField = _self.getInfo.extension.model.value
                const textField = _self.getInfo.extension.fieldKey.value
                const modelTree = vm.$store.state.preview.pages[vm.$store.state.preview.nowPageId]
                let _formModule = getModelByModelTree(_self.formId, modelTree)
                modelField && _formModule && (_formModule.data.formData[modelField] = '')
                textField && _formModule && (_formModule.data.formData[textField] = '')
                // _self.setFormDataModel('')
              }
              //开关按钮单独获取值
              if (_self.getInfo.type === 'uiSwitch') {
                let switchValue = null
                if (_self.getInfo.extension.defaultState.value) {
                  switchValue = _self.getInfo.extension.activeValue.value
                } else {
                  switchValue = _self.getInfo.extension.inactiveValue.value
                }
                _self.setFormDataModel(switchValue)
              } else if (_self.getInfo.type === 'uiDate') {
                // 日期时间组件单独获取值
                if (_self.getInfo.extension.defaultDate.value) {
                  // 如果设置默认值为 当前
                  if (_self.getInfo.extension.selection.value === 'datetime' || _self.getInfo.extension.selection.value === 'date') {
                    // 如果当前组件类型为 datetime 或 date
                    if (_self.getInfo.extension.defaultDate.value === 'now') {
                      // 设置 默认值为 当前 的默认值
                      let defaultDate = getDefaultDate('now')
                      // 如果为日期类型则去掉值的时分秒
                      if (_self.getInfo.extension.selection.value.indexOf('time') == -1) {
                        _self.setFormDataModel(defaultDate.split(' ')[0])
                      } else {
                        _self.setFormDataModel(defaultDate)
                      }
                    } else {
                      _self.setFormDataModel('')
                    }
                  } else if (_self.getInfo.extension.selection.value === 'datetimerange' || _self.getInfo.extension.selection.value === 'daterange') {
                    // 如果当前组件类型为 时间范围
                    if (_self.getInfo.extension.defaultDate.value !== 'now') {
                      let defaultDate = getDefaultDate(_self.getInfo.extension.defaultDate.value)
                      // 如果为日期类型则去掉值的时分秒
                      if (_self.getInfo.extension.selection.value.indexOf('time') == -1) {
                        defaultDate[0] = defaultDate[0].split(' ')[0]
                        defaultDate[1] = defaultDate[1].split(' ')[0]
                      }
                      _self.setFormDataModel(defaultDate)
                    } else {
                      _self.setFormDataModel('')
                    }
                  }
                } else {
                  // 没有默认值则置为空
                  _self.setFormDataModel('')
                }
              } else {
                _self.setFormDataModel(getComponentDefaulType(_self.getInfo.type))
              }
            }
          }

          // 清除_formData 中的老值
          const _formData = vm.vuiComponents[_self.formId]?.self.getInfo?.data?._formData || {}
          if (!isEmpty(_formData)) {
            _formData[_self.getInfo.extension?.model?.value] =
              vm.vuiComponents[_self.formId]?.self.getInfo?.data?.formData[[_self.getInfo.extension?.model?.value]]
          }

          // _formData[_self.getInfo.extension?.model?.value] = getComponentDefaulType(_self.getInfo.type)
          // 触发组件的值改变事件
          _self.onChange && _self.onChange()
        }
      }
    })

  done(null, {})
}
